package org.kite.processor;

import org.kite.MatchResult;

import java.text.MessageFormat;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UnorderedListProcessor extends ProcessorBase {

	String regex = "^\\-\\s+(?<content>.+)$";

	@Override
	public MatchResult match(String line) {
		boolean isMatch = Pattern.matches(regex, line);
		String msg = MessageFormat.format("[UnorderedListProcessor] matches [{0}] : {1}",
				line, isMatch);
		System.out.println(msg);
		return isMatch ? MatchResult.MatchAndContinue : MatchResult.NotMatch;
	}

	@Override
	public String process(List<String> lines) throws Exception {
		Pattern p = Pattern.compile(regex);
		StringBuilder sb = new StringBuilder();
		sb.append("<ul>");
		for (String line : lines) {
			Matcher matcher = p.matcher(line);
			if (matcher.find()) {
				String content = matcher.group("content");
				sb.append(MessageFormat.format("<li>{0}</li>", content));
			}
		}
		sb.append("</ul>");
		String htmlContent = sb.toString();
		String msg = MessageFormat.format("[UnorderedListProcessor] procces [{0} --> [{1}]]",
				String.join(",", lines),
				htmlContent);
		System.out.println(msg);
		return htmlContent;
	}
}
